package win.bigwhale.sudoku.view;

import java.awt.Point;
import java.io.File;
import java.nio.channels.NonWritableChannelException;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.shape.Rectangle;
import win.bigwhale.sudoku.SudokuUtil;
import win.bigwhale.sudoku.logic.SudokuMatrix;
import win.bigwhale.sudoku.sound.SoundPlayer;
import win.bigwhale.sudoku.sound.SoundPlayerImp;

@SuppressWarnings("unused")
public class SelectedRect {
	private static SelectedRect selectedRect = new SelectedRect();
	private Rectangle rectangle = null;
	private Point rectLocation = null;
	private SoundPlayer warningSoundPlayer = null;

	private SelectedRect() {
		rectangle = new Rectangle(49.0, 49.0);
		rectLocation = new Point();
		warningSoundPlayer = new SoundPlayerImp(new MediaPlayer(new Media(new File(SudokuUtil.WARNING_SOUND_PATH).toURI().toString())));
	}

	public static SelectedRect getSelectedRectShow() {
		return selectedRect;
	}

	public void reLocation(Point point) {
		rectLocation.setLocation(point.getX(), point.getY());
		rectangle.setLayoutX(point.getX());
		rectangle.setLayoutY(point.getY());
		rectangle.setVisible(true);
	}

	public void setVisible(boolean visible) {
		rectangle.setVisible(visible);
	}

	public Rectangle getRectangle() {
		return rectangle;
	}

	public Point getRectLocation() {
		return rectLocation;
	}

	public void setFill(SudokuMatrix sudokuMatrix, Point selectedPosition) {
		if (sudokuMatrix.isOriginNumberPosition(selectedPosition)) {
			rectangle.setFill(SudokuUtil.WARNING_SELECT_COLOR);
			warningSoundPlayer.stop();
			warningSoundPlayer.play();
		} else {
			rectangle.setFill(SudokuUtil.SELECT_RECT_COLOR);
		}
	}
}
